package com.icore.bean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.icore.AppException;

/**
 * 实体基类
 * 
 */
public abstract class BaseEntity {
	public final static String UTF8 = "UTF-8";

	private int resCode;
	private String resMessage;

	public int getResCode() {
		return resCode;
	}

	public void setResCode(int resCode) {
		this.resCode = resCode;
	}

	public String getResMessage() {
		return resMessage;
	}

	public void setResMessage(String resMessage) {
		this.resMessage = resMessage;
	}

	protected static String readByByte(InputStream dis, long len, ByteArrayOutputStream buffer) throws AppException {
		int bufferSize = 512;
		byte byteInput[] = new byte[bufferSize];
		int size = 0;
		if (len != -1) {
			long askSize = 0;
			while (len > 0) {
				askSize = (len < (long) bufferSize) ? len : (long) bufferSize;
				try {
					if ((size = dis.read(byteInput, 0, (int) askSize)) != -1) {
						len -= size;
						buffer.write(byteInput, 0, size);
					} else {
						break;
					}
				} catch (IOException e) {
					// IO流异常
					e.printStackTrace();
					throw AppException.io(e);
				}
			}
		} else {
			// if the content-length is not available
			try {
				while ((size = dis.read(byteInput, 0, bufferSize)) != -1) {
					buffer.write(byteInput, 0, size);
				}
			} catch (IOException e) {
				// IO流异常
				e.printStackTrace();
				throw AppException.io(e);
			}
		}

		try {
			byte[] resultByts = buffer.toByteArray();
			return new String(resultByts, UTF8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
